package cn.kgc.springbootdemo.util;


import cn.kgc.springbootdemo.constant.CommonConstants;
import cn.kgc.springbootdemo.constant.FileConstants;
import lombok.experimental.UtilityClass;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 文件大小判断工具类
 * </p>
 *
 * @author dzj
 * @Version: V1.0
 * @since 2019-10-31 14:49
 */

@UtilityClass
public class FileCheckUtils {

    /**
     * 判断文件大小
     *
     * @param len  上传文件大小
     * @param size 限制大小
     * @param unit 限制单位（B,K,M,G）
     * @return 符合满足条件返回true、否则返回false
     */
    public Boolean checkFileSize(Long len, int size, String unit) {
        double fileSize = 0;
        if (FileConstants.FILE_SIZE_B.equals(unit.toUpperCase())) {
            fileSize = (double) len;
        } else if (FileConstants.FILE_SIZE_K.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
        } else if (FileConstants.FILE_SIZE_M.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
        } else if (FileConstants.FILE_SIZE_G.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > size) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 根据文件大小计算对应单位
     *
     * @param len : 文件大小
     * @return 返回对应单位
     */
    public String getFileSizeToM(Long len, String unit) {
        double fileSize = 0;
        if (FileConstants.FILE_SIZE_B.equals(unit.toUpperCase())) {
            fileSize = (double) len;
            return fileSize + FileConstants.FILE_SIZE_B;
        } else if (FileConstants.FILE_SIZE_K.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1024;
            return fileSize + FileConstants.FILE_SIZE_K;
        } else if (FileConstants.FILE_SIZE_M.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1048576;
            return fileSize + FileConstants.FILE_SIZE_M;
        } else if (FileConstants.FILE_SIZE_G.equals(unit.toUpperCase())) {
            fileSize = (double) len / 1073741824;
            return fileSize + FileConstants.FILE_SIZE_G;
        }
        return "0";
    }

    /**
     * https://blog.csdn.net/yyj108317/article/details/88793099
     *
     * @param fileName : 文件名
     * @param request  : HttpServletRequest
     * @Description: 导出文件转换文件名称编码
     */
    public String encodeFileName(String fileName, HttpServletRequest request) {
        String codedFilename = null;
        try {
            String agent = request.getHeader("USER-AGENT");
            // ie浏览器及Edge浏览器
            if (null != agent && -1 != agent.indexOf("MSIE") || null != agent && -1 != agent.indexOf("Trident") || null != agent && -1 != agent.indexOf("Edge")) {
                String name = java.net.URLEncoder.encode(fileName, "UTF-8");
                codedFilename = name;
            } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
                // 火狐,Chrome等浏览器
                codedFilename = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return codedFilename + CommonConstants.EXCEL_SUFFIX;
    }
}
